<?php
class MapController extends Zend_Controller_Action {
	
	
	public function indexAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
		
	}
	
	/**
	 * Prepare placemark, which means take in mouse click from map and save placemarks in session to be saved.
	 */
	public function addplacemarkAction() {
        $this->_helper->viewRenderer->setNoRender();   //view info disabled
		$lat = $this->_getParam("lat");
        $lng = $this->_getParam("lng");
        $photoTitle = $this->_getParam("name");
        $fileUrl = $this->_getParam("fileUrl");
        
		$mySession = new Zend_Session_Namespace(Constant::SESSION_NAME);
		
		$placeMark = new PlaceMarkDTO();
		$placeMark->latitude = $lat;
		$placeMark->longitude = $lng;
		$placeMark->name = $photoTitle;
		$placeMark->fileUrl = $fileUrl;
		$placeMark->created = $this->_helper->generator->generateCurrentTime();
		$placeMark->modified = $this->_helper->generator->generateCurrentTime();
		if (isset($mySession->placeMarks)) {
			if ($this->isNewPlaceMark($placeMark, $mySession->placeMarks)) {
				$currentCount = count($mySession->placeMarks);
				$mySession->placeMarks[$currentCount++] = $placeMark;
			}
		} else {
			$mySession->placeMarks = array();
			$mySession->placeMarks[0] = $placeMark;
		}
		
		$info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
		$returnJson = array(
		  "info" => $info,
		  "map" => $mySession->placeMarks
		);
		echo Zend_Json::encode($returnJson);
	}
	
	/**
	 * Check if the placeMark already exist in the session.
	 * 
	 * @param $placeMark
	 * @param $placeMarks
	 * @return string
	 */
	private function isNewPlaceMark($placeMark, $placeMarks) {
		$ret = true;
		foreach ($placeMarks as $entry) {
			if ($entry->latitude == $placeMark->latitude && $entry->longitude == $placeMark->longitude) {
				$ret = false;
				break;
			}
		}
		return $ret;
	}
	
	
	/**
	 * Edit one placemark by given position parameter.
	 */
	public function editplacemarkAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
//		$this->_helper->layout()->disableLayout();
		$lat = $this->_getParam("lat");
		$lng = $this->_getParam("lng");
		$name = $this->_getParam("name");
		$description = $this->_getParam("description");
		
		$mySession = new Zend_Session_Namespace('mySession');
		
		foreach ($mySession->placeMarks as $placeMark) {
			if ($placeMark->latitude == $lat && $placeMark->longitude == $lng) {
				$placeMark->name = $name;
				$placeMark->description = $description;
				$placeMark->modified = $this->_helper->generator->generateCurrentTime();
				break;
			}
		}
		
		$info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
		
		$mySession->info = $info;
		$returnJson = array(
		    "map" => $mySession->placeMarks,
		    "info" => $info
		);
//		$placeMarkToUpdate = new PlaceMarkDTO();
//		$placeMarks = $this->updatePlaceMarks($mysSession->placeMarks, $placeMarkToUpdate);
		echo Zend_Json::encode($returnJson);
	}
	
	/**
	 * Remove place mark by given lat and lng.
	 */
	public function removeplacemarkAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
        $lat = $this->_getParam("lat");
        $lng = $this->_getParam("lng");
        $mySession = new Zend_Session_Namespace('mySession');
        
        $index = 0;
        $placeMarks = array();
        foreach($mySession->placeMarks as $placeMark) {
        	if ($placeMark->latitude == $lat && $placeMark->longitude == $lng) {
        		continue;
        	}
        	$placeMarks[$index++] = $placeMark;
        }
        $mySession->placeMarks = $placeMarks;
        $info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
        $returnJson = array(
          "map" => $mySession->placeMarks,
          "info" => $info
        );
        echo Zend_Json::encode($returnJson);
	}
	
	/**
	 * Clear all placemarks action.
	 */
	public function resetAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
		$mySession = new Zend_Session_Namespace('mySession');
		$mySession->placeMarks = array();
		$mySession->trip = null;
		$mySession->step = TripStep::APP_START;
		$mySession->info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
		
		$returnJson = array(
		  "map" => $mySession->placeMarks,
		  "info" => $mySession->info
		);
		echo Zend_Json::encode($returnJson);
	}
	
	/**
	 * Update placeMark by looking up in placeMarks list.
	 * 
	 * @param $placeMarks
	 * @param $placeMarkToUpdate
	 * @return return updated placeMarks
	 */
	private function updatePlaceMarks($placeMarks, $placeMarkToUpdate) {
		
	}
	
	/**
	 * Create trip action.
	 * Input parameters:
	 * title, description, public, step(describe with step of trip creation now is).
	 * step 1: create inital trip and enable user to add placemark on map.
	 */
	public function createtripAction() {
		$mySession = new Zend_Session_Namespace('mySession');
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
		$title = trim($this->_getParam("title"));
		$description = trim($this->_getParam("description"));
		$public = $this->_getParam("public");
		$step = $this->_getParam("step");
		if ($step == TripStep::TRIP_INIT) {
			$tripDTO = new TripDTO();
			$tripDTO->title = $title;
			$tripDTO->description = $description;
			$tripDTO->public = $public;
			$tripDTO->user = $this->_helper->user->getUserData();
			$tripDTO->created = $this->_helper->generator->generateCurrentTime();
			$tripDTO->modified = $this->_helper->generator->generateCurrentTime();
			$mySession->trip = $tripDTO;
			$mySession->placeMarks = array();
			$mySession->step = TripStep::TRIP_INIT;
		}
		
		$info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
		$mySession->info = $info;
		$mySession->placeMarks = array();
		$returnJson = array(
		  "info" => $info,
		  "map" => array()
		);
		echo Zend_Json::encode($returnJson);
	}
	
	/**
	 * Get trip info, i.e. placemarks, left container info and etc.
	 */
	public function getinfoAction() {
		$this->_helper->viewRenderer->setNoRender();   //view info disabled
		$mySession = new Zend_Session_Namespace('mySession');
		$info = $this->view->renderMapInfo($mySession->placeMarks, $mySession->trip, $mySession->step);
		$mySession->info = $info;
        $returnJson = array(
          "map" => $mySession->placeMarks,
          "info" => $mySession->info
        );
        echo Zend_Json::encode($returnJson);
	}
}
?>